package si.yun.web;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import si.yun.entity.SyCarousel;
import si.yun.service.ElasticSearchService;
import si.yun.service.MicroCircleService;
import si.yun.utils.JsonData;
import si.yun.utils.TestAliUpload;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: 微圈分类web层
 * @Description: TODO
 * @Author: 李振海
 * @Date 2021/10/18 10:06
 */
@Slf4j
@RestController
@RequestMapping("/microCircle")
@PropertySource({"config/config.properties"})
public class MicroCircleController {

    @Resource(name="MicroCircleServiceImpl")
    private MicroCircleService microCircleService;

    /*获取图片服务器路径*/
    @Value("${path1}")
    private String path;

    //服务器上传文件
    @Autowired
    private TestAliUpload testAliUpload;

    //连接redis缓存
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ElasticSearchService elasticSearchService;

    /**
     * @return si.yun.utils.JsonData
     * @Description 获取微圈分类数据
     * @Date 2021/10/12 20:58
     **/
    @GetMapping("/selForumClass")
    public JsonData selForumClass(){
        List<Map<String, Object>> ForumClass = microCircleService.selForumClass();
//        redis缓存中获取
//        ValueOperations<String,String> vo = redisTemplate.opsForValue();
//        String value = vo.get("selForumClass");
//        Gson gson = new Gson();
//        List<Map<String , Object>> ForumClass = gson.fromJson(value, List.class);
        return ForumClass!=null?JsonData.buildSuccess(ForumClass):JsonData.buildError("服务器错误");
    }



    /**
     * @return si.yun.utils.JsonData
     * @Description 通过微圈分类id获取该微圈分类下的数据
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("selMicrocircleDetailsList")
    public JsonData selMicrocircleDetailsList(Integer id) {
       List<Map<String, Object>> ForumClass = microCircleService.selMicrocircleDetailsList(id);
//        ValueOperations<String,String> vo = redisTemplate.opsForValue();
//        String value = vo.get("updMicrocircleDetailsList"+String.valueOf(id));
//        Gson gson = new Gson();
//        List<Map<String , Object>> ForumClass = gson.fromJson(value, List.class);
        return ForumClass!=null?JsonData.buildSuccess(ForumClass):JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 通过微圈id获取微圈详情页面的具体信息
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("selMicrocircleDetails")
    public JsonData selMicrocircleDetails(Integer id) {
        Map<String, Object> ForumClass = microCircleService.selMicrocircleDetails(id);
        ForumClass.put("path", path);
        return ForumClass != null ? JsonData.buildSuccess(ForumClass) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 通过微圈id获取微圈下相对应的评论
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("selCommentData")
    public JsonData selCommentData(Integer id) {
        Map<String, Object> map = new HashMap();
        List<Map<String, Object>> ForumClass = microCircleService.selCommentData(id);
        map.put("path", path);
        map.put("ForumClass", ForumClass);
        return ForumClass != null ? JsonData.buildSuccess(map) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 对当前微圈进行评论
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("addComment")
    public JsonData addComment(@RequestParam Map<String, Object> map) {
        int ForumClass = microCircleService.addComment(map);
        return ForumClass != 0 ? JsonData.buildSuccess(map) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 判断当前登录的用户是否已经收藏查看的微圈
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("selCollection")
    public JsonData selCollection(@RequestParam Map<String, Object> map) {
        Map<String, Object> ForumClass = microCircleService.selCollection(map);
        return ForumClass != null ? JsonData.buildSuccess(ForumClass) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 当前登录用户收藏此条微圈
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("addCollection")
    public JsonData addCollection(@RequestParam Map<String, Object> map) {
        int ForumClass = microCircleService.addCollection(map);
        return ForumClass != 0 ? JsonData.buildSuccess(ForumClass) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 当前登录用户取消收藏此条微圈
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("DelCancelCollection")
    public JsonData DelCancelCollection(@RequestParam Map<String, Object> map) {
        int ForumClass = microCircleService.DelCancelCollection(map);
        return ForumClass != 0 ? JsonData.buildSuccess(ForumClass) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 当前登录用户取消点赞此条微圈
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("DelFabulous")
    public JsonData DelFabulous(@RequestParam Map<String, Object> map) {
        int ForumClass = microCircleService.DelFabulous(map);
        return ForumClass != 0 ? JsonData.buildSuccess(ForumClass) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 当前登录用户点赞此条微圈
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("addFabulous")
    public JsonData addFabulous(@RequestParam Map<String, Object> map) {
        int ForumClass = microCircleService.addFabulous(map);
        return ForumClass != 0 ? JsonData.buildSuccess(ForumClass) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 获取该微圈分类下相关的推荐
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("selRecommend")
    public JsonData selRecommend(Integer id) {
        List<Map<String, Object>> Recommend = microCircleService.selRecommend(id);
        return Recommend != null ? JsonData.buildSuccess(Recommend) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 获取微圈分类数据
     * @Date 2021/10/12 20:58
     **/
    @GetMapping("selReleaseMicroClassData")
    public JsonData selReleaseMicroClassData() {
        List<String> ReleaseMicroClassData = microCircleService.selReleaseMicroClassData();
        return ReleaseMicroClassData != null ? JsonData.buildSuccess(ReleaseMicroClassData) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 发布微圈
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("addReleaseMicroData")
    public JsonData addReleaseMicroData(@RequestParam Map<String, Object> map) {
        int ReleaseMicroData = microCircleService.addReleaseMicroData(map);
        return ReleaseMicroData != 0 ? JsonData.buildSuccess(ReleaseMicroData) : JsonData.buildError("服务器错误");
    }

    /**
     * @Description  发布微圈(className微圈名称,microData微圈内容,userId用户id,title标题)
     * @return si.yun.utils.JsonData
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("/addReleaseMicroImg")
    public JsonData addReleaseMicroImg(@RequestParam("file") MultipartFile files) throws Exception {

        String newName = uploadFile(files);
        return newName != null ? JsonData.buildSuccess(newName) : JsonData.buildError("服务器异常！请耐心等待修复！");
    }

    public String  uploadFile(@RequestParam("file") MultipartFile files) throws IllegalStateException, IOException {
        //上传到服务器
        String newName = testAliUpload.upload(files);
        return newName;
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description es微圈查询
     * @Date 2021/10/12 20:58
     **/
    @RequestMapping("/selEsData")
    public JsonData selEsData(String index, String title,String className,String content, int page, int size) {
        //将搜索内容去和搜索关键字进行匹配，如果相匹配搜索关键字表的搜索次数加1
        int i = microCircleService.selEsData(content);
        Map<String,Object> map = new HashMap<>();
        List<Map<String, Object>> list = null;
        try {
            list = elasticSearchService.searchPage
                    (index,title,className,content,page,size);
            map.put("path",path);
            map.put("hits",list);
        } catch (IOException e) {
            e.printStackTrace();
            map = null;
        }
        return JsonData.buildSuccess(map);
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 获取热门搜索记录
     * @Date 2021/10/12 20:58
     **/
    @GetMapping("/selPopularSearch")
    public JsonData selPopularSearch() {
        List<Map<String, Object>> selPopularSearch = microCircleService.selPopularSearch();
        return selPopularSearch != null ? JsonData.buildSuccess(selPopularSearch) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 发布微圈
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("updReleaseMicroData")
    public JsonData updReleaseMicroData(@RequestParam Map<String, Object> map) {
        int ReleaseMicroData = microCircleService.updReleaseMicroData(map);
        return ReleaseMicroData != 0 ? JsonData.buildSuccess(ReleaseMicroData) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 查询微圈
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("selReleaseMicroData")
    public JsonData selReleaseMicroData(@RequestParam Map<String, Object> map) {
        int ReleaseMicroData = microCircleService.updReleaseMicroData(map);
        return ReleaseMicroData != 0 ? JsonData.buildSuccess(ReleaseMicroData) : JsonData.buildError("服务器错误");
    }


    /**
     * @return si.yun.utils.JsonData
     * @Description 查询路径
     * @Date 2021/10/12 20:58
     **/
    @GetMapping("selPathData")
    public JsonData selPathData() {

        return path != null ? JsonData.buildSuccess(path) : JsonData.buildError("服务器错误");
    }

    /**
     * @return si.yun.utils.JsonData
     * @Description 微圈分享+1
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("addMicroCircleSharing")
    public JsonData addMicroCircleSharing(Integer id) {
        Integer i=microCircleService.addMicroCircleSharing(id);
        return i != 0 ? JsonData.buildSuccess(i) : JsonData.buildError("服务器错误");
    }
    /**
     * @return si.yun.utils.JsonData
     * @Description 删除个人评论 更改状态为删除
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("updCommentStatusDelwq")
    public JsonData updCommentStatusDelwq(Integer commentId,Integer userId,Integer commodityId) {
        Integer i=microCircleService.updCommentStatusDelwq(commentId,userId,commodityId);
        return i != 0 ? JsonData.buildSuccess(i) : JsonData.buildError("服务器错误");
    }
    /**
     * @return si.yun.utils.JsonData
     * @Description 评论点赞
     * @Date 2021/10/12 20:58
     **/
    @PostMapping("addDetailsLike")
    public JsonData addDetailsLike(Integer commentId) {
        Integer i=microCircleService.addDetailsLike(commentId);
        return i != 0 ? JsonData.buildSuccess(i) : JsonData.buildError("服务器错误");
    }



}
